#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll T;
ll n;
ll a[200005],lst[200005],id[1000005];
ll dp[200005];
ll sum[200005];
struct Node{
	ll l,r;
	ll val,lazy,flag;
}Tree[800005];
void Build(ll id,ll l,ll r){
	Tree[id].l=l,Tree[id].r=r;
	if(l==r) return;
	ll mid=(l+r)>>1;
	Build(id*2,l,mid);
	Build(id*2+1,mid+1,r);
	Tree[id].val=Tree[id*2].val+Tree[id*2+1].val;
}
void Lazy(ll id){
	if(Tree[id].flag==0) return;
	Tree[id*2].val=(Tree[id*2].r-Tree[id*2].l+1)*Tree[id].lazy;
	Tree[id*2+1].val=(Tree[id*2+1].r-Tree[id*2+1].l+1)*Tree[id].lazy;
	Tree[id*2].lazy=Tree[id].lazy;
	Tree[id*2+1].lazy=Tree[id].lazy;
	Tree[id*2].flag=Tree[id*2+1].flag=1;
	Tree[id].flag=0;
}
void Change(ll id,ll l,ll r,ll val){
	if(l<=Tree[id].l&&Tree[id].r<=r){
		Tree[id].lazy=val;
		Tree[id].val=(Tree[id].r-Tree[id].l+1)*val;
		Tree[id].flag=1;
		return;
	}
	Lazy(id);
	ll mid=(Tree[id].l+Tree[id].r)>>1;
	if(l<=mid) Change(id*2,l,mid,val);
	if(r>mid) Change(id*2+1,mid+1,r,val);
	Tree[id].val=Tree[id*2].val+Tree[id*2+1].val;
}
ll Query(ll id,ll l,ll r){
	if(l<=Tree[id].l&&Tree[id].r<=r) return Tree[id].val;
	Lazy(id);
	ll ans=0,mid=(Tree[id].l+Tree[id].r)>>1;
	if(l<=mid) ans+=Query(id*2,l,r);
	if(r>mid) ans+=Query(id*2+1,l,r);
	return ans;
}
void Clean(){
	memset(dp,0,sizeof(dp));
	memset(id,0,sizeof(id));
	memset(sum,0,sizeof(sum));
	memset(lst,0,sizeof(lst));
	memset(Tree,0,sizeof(Tree));
}
ll Count(ll r){
	ll ans=0;
	for(ll i=1;i<=r;i++){
		if(lst[i]==0) continue;
		if(lst[i]==i-1){
			ans+=a[i];
			continue;
		}
		ll num1=Query(1,lst[i],lst[i]);
		ll num2=Query(1,i,i);
		ll num3=Query(1,lst[i]+1,i-1);
		ll num4=(i-lst[i])-1;
		if(num1==num2&&((num3==0&&num1==1)||(num3==num4&&num1==0))) ans+=a[i];
	}
	return ans;
}
int main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	scanf("%lld",&T);
	while(T--){
		Clean();
		scanf("%lld",&n);
		for(ll i=1;i<=n;i++){
			scanf("%lld",&a[i]);
			if(!id[a[i]]) id[a[i]]=i;
			else lst[i]=id[a[i]],id[a[i]]=i;
			if(a[i]==a[i-1]) sum[i]=a[i];
			sum[i]+=sum[i-1];
		}
		Build(1,1,n);
		for(ll i=1;i<=n;i++){
			if(lst[i]==0){
				dp[i]=dp[i-1];
				continue;
			}
			if(lst[i]+1==i){
				dp[i]=dp[i-1]+a[i];
				Change(1,i,i,Query(1,lst[i],lst[i]));
				continue;
			}
			ll cor=Query(1,lst[i],lst[i]);//lst's cor
			ll ans1=(sum[i-1]-sum[lst[i]+1])+dp[lst[i]];//range ans
			ll cor1=0;
			if(cor1==cor) cor1=1;// cor that range change to
			if(lst[lst[i]+1]){//range's first's lst
				ll cor2=Query(1,lst[lst[i]+1],lst[lst[i]+1]);//range's first's lst's cor
				if(cor1==cor2){
					ll sum1=Query(1,lst[lst[i]+1]+1,lst[i]);//cor in range's frist's range
					ll num1=(lst[i]+1-lst[lst[i]+1])-1;
					if((num1==sum1&&cor2==0)||(sum1==0&&cor2==1)) ans1+=a[lst[i]+1];
				}
			}//ans1: num after change
			if(ans1+a[i]>dp[i-1]){
				dp[i]=ans1+a[i];
				Change(1,lst[i]+1,i-1,cor1);
				Change(1,i,i,cor);
			}
			else dp[i]=dp[i-1];
		}
		printf("%lld\n",dp[n]);
	}
	return 0;
}
